/**
 * Created by IntelliJ IDEA.
 * User: Bezdelnick
 * Date: 10.09.12
 * Time: 14:29
 * To change this template use File | Settings | File Templates.
 */

var enableGettingCaretPositionInEditor = false;
var btnUploadImage = '';
var richEditor = '';
var g_caretPosition = 0;
var spanCaretPosition = '';
//var editorElement;

setInterval(function() {
    if (enableGettingCaretPositionInEditor && richEditor.length > 0) {
        g_caretPosition = getCaretPositionMyMethod(richEditor);
        if (spanCaretPosition.length > 0)
            spanCaretPosition.text(g_caretPosition);
    }
}, 500);

function getCaretPositionMyMethod(element) {
    var position = 0;

    if (element.length > 0) {
        if (element.selectionStart) {
            position = element.selectionStart;
        } else if (document.selection) {
            element.focus();
            var Sel = document.selection.createRange();
            var SelLength = document.selection.createRange().text.length;
            Sel.moveStart('character', -element.value.length);
            position = Sel.text.length - SelLength;
        } else if (window.getSelection) {
            var sel = window.getSelection();
            if (sel.rangeCount) {
                var range = sel.getRangeAt(0);
                position = range.endOffset;
                //editorElement = sel.getElement();
                if (range.commonAncestorContainer.parentNode == element) {
                    position = range.endOffset;
                }
            }
        } else if (document.getSelection) {
            var sel = document.getSelection();
            if (sel.rangeCount) {
                var range = sel.getRangeAt(0);
                position = range.endOffset;
                if (range.commonAncestorContainer.parentNode == element) {
                    position = range.endOffset;
                }
            }
        } else if (document.selection && document.selection.createRange) {
            var range = document.selection.createRange();
            if (range.parentElement() == element) {
                var tempEl = document.createElement("span");
                element.insertBefore(tempEl, element.firstChild);
                var tempRange = range.duplicate();
                tempRange.moveToElementText(tempEl);
                tempRange.setEndPoint("EndToEnd", range);
                position = tempRange.text.length;
            }
        }
    }
    return position;
}

function pasteImageToEditor(imgUrl) {
    if (richEditor.length > 0) {
//        var first = richEditor.html().substr(0, g_caretPosition);
//        var second = richEditor.html().substr(g_caretPosition, richEditor.html().length - g_caretPosition);
//        richEditor.html(first + ' <img src="' + imgUrl + '" height="150"/> ' + second);

        if (richEditor.html) {
            var first = richEditor.html().substr(0, g_caretPosition);
            var second = richEditor.html().substr(g_caretPosition, richEditor.html().length - g_caretPosition);
            richEditor.html(first + ' <img src="' + imgUrl + '" height="150"/> ' + second);
        } else if (richEditor.text) {
            var first = richEditor.text().substr(0, g_caretPosition);
            var second = richEditor.text().substr(g_caretPosition, richEditor.text().length - g_caretPosition);
            richEditor.text(first + ' <img src="' + imgUrl + '" height="150"/> ' + second);
        }
        return true;
    } else
        return false;
}

$(document).ready(function() {
    $('body').on({
        mouseover: function() {
            if (btnUploadImage.length > 0 && richEditor.length > 0 && spanCaretPosition.length > 0) {
                btnUploadImage.replaceWith(document.getElementById('btn_upload_img_1'));
                $('body').off('mouseover');
            } else {
                if (!btnUploadImage.length > 0) {
                    $('div.nicEdit-panel').after('<span wicket:id="btn_upload_img" id="btn_upload_img" href="#"></span>');
                    btnUploadImage = $('#btn_upload_img');
                }
                if (!richEditor.length > 0) {
                    richEditor = $('div.nicEdit-main[contenteditable=true]');
                    richEditor.on({
                        focusin: function() {
                            enableGettingCaretPositionInEditor = true;
                        },
                        focusout: function() {
                            enableGettingCaretPositionInEditor = false;
                        }
                    });
                }
                if (!spanCaretPosition.length > 0) {
                    spanCaretPosition = $('span#caret_position');
                }
            }
        }
    });
});